package com.eventdemo.controller;

import com.eventdemo.entity.Event;
import com.eventdemo.entity.EventCopy;
import com.eventdemo.service.EventCopyService;
import com.eventdemo.service.EventService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.web.bind.annotation.*;

import javax.persistence.criteria.Predicate;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@RestController(value = "/event")
@CrossOrigin(originPatterns = "*", methods = {RequestMethod.GET, RequestMethod.POST, RequestMethod.DELETE})
public class EventController {

    @Autowired
    private EventService eventService;

    @Autowired
    private EventCopyService copyService;

    @GetMapping("/page")
    public List<Event> page(@RequestParam(value = "createUser", required = false) String createUser, @RequestParam(value = "createTime", required = false) String createTime) {
        List<Event> eventList = eventService.findAll((Specification<Event>) (root, criteriaQuery, cb) -> {
            List<Predicate> predicates = new ArrayList<>();
            if (createUser != null && !createUser.equals("")) {
//                predicates.add(cb.like(root.get("createUser").as(String.class), "%" + createUser + "%"));
                predicates.add(cb.equal(root.get("deleteFlag").as(Integer.class), 0));
                predicates.add(cb.equal(root.get("createUser").as(String.class), createUser));
            }
            if (createTime != null && !createTime.equals("")) {
                predicates.add(cb.equal(root.get("eventCreateTime").as(LocalDate.class), LocalDate.parse(createTime)));
            }
            return cb.and(predicates.toArray(new Predicate[predicates.size()]));
        });
        return eventList;
    }

    @GetMapping("/users")
    public List<Event> users() {
        List<Event> eventList = eventService.findAll();
        if (!eventList.isEmpty()) {
            List<Event> events = new ArrayList<>();
            eventList.stream().collect(Collectors.groupingBy(Event::getCreateUser)).keySet().stream().map(key -> key).collect(Collectors.toList()).forEach(user -> {
                Event event = Event.builder()
                        .createUser(user)
                        .build();
                events.add(event);
            });
            return events;
        }
        return new ArrayList<>();
    }

    @DeleteMapping("/remove")
    public Integer remove(@RequestParam("id") Integer id) {
        eventService.deleteById(id);
        return 200;
    }

    @PostMapping("/saveAndFlush")
    public Integer saveAndFlush(@RequestBody Event event) {
        eventService.saveAndFlush(event);
        if (event.getId() == null) {
            EventCopy copy = EventCopy.builder()
                    .eventCreateTime(event.getEventCreateTime())
                    .advanceAmount(event.getAdvanceAmount())
                    .eventContent(event.getEventContent())
                    .cashAmount(event.getCashAmount())
                    .createUser(event.getCreateUser())
                    .remark(event.getRemark())
                    .remedialPlan(event.getRemedialPlan())
                    .build();
            copyService.saveAndFlush(copy);
        }
        return 200;
    }


}
